package algorithm;

public class LRU {
    public class Node<T> {
        public T value;
        public Node<T> last;
        public Node<T> next;
        public Node(T value) {
            this.value = value;
        }
    }

    public class NodeDoubleLinkedList<V> {
        public Node<V> head;
        public Node<V> tail;

        public NodeDoubleLinkedList() {
            head = null;
            tail = null;
        }

        public void addNode(Node<V> newNode) {
            if (null == newNode) {
                return;
            }

            if (null == head) {
                head = newNode;
                tail = newNode;
            } else {
                tail.next = newNode;
                newNode.last = tail;
                tail = newNode;
            }
        }

        public void moveNodeToTail(Node<V> node) {
            // 就是尾部
            if (tail == node) {
                return;
            }
            // 就是头部
            if (head == node) {
                head = head.next;
                head.last = null;
            } else {
                node.last.next = node.next;
                node.next.last = node.last;
            }
            node.last = tail;
            node.next = null;
            tail.next = node;
            tail = node;
        }

        public Node<V> removeHead() {
            if (null == head) {
                return null;
            }

            Node<V> res = this.head;
            if (head == tail) {
                head = null;
                tail = null;
            } else {
                head = res.next;
                res.next = null;
                head.last = null;
            }
            return res;
        }
    }

    public static void main(String[] args) {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {

            }
        });
    }

}
